#include <algorithm>
#include "debug.h"
#include "Utils\\ResGroupManager.h"

NS_BEGIN(SDL)
NS_BEGIN(UTILS)

#pragma region structure Resource
#pragma region CTOR & DTOR

Resource::Resource(std::string const& _name, std::string const& _path)
	: Name(_name), Path(_path)
{
	CTOR("Resource", this);
}

Resource::Resource(Resource const& _resource)
{
	CTOR("Resource", this);
	*this = _resource;
}

Resource::~Resource()
{
	DTOR("Resource", this);
}

#pragma endregion
#pragma region Operators

Resource& Resource::operator=(Resource const& _resource)
{
	if (this != &_resource)
	{
		Path = _resource.Path;
		Name = _resource.Name;
	}
	return (*this);
}

bool Resource::operator==(Resource const& _resource) const
{
	return (Path == _resource.Path && Name == _resource.Name);
}

bool Resource::operator!=(Resource const& _resource) const
{
	return (Path != _resource.Path || Name != _resource.Name);
}

#pragma endregion
#pragma endregion
#pragma region classe ResGroup
#pragma region CTOR & DTOR

ResGroup::ResGroup(std::string const& _name)
	: m_name(_name)
{
	CTOR("ResGroup", this);
}

ResGroup::ResGroup(ResGroup const&)
{
}

ResGroup::~ResGroup()
{
	DTOR("ResGroup", this);
}

#pragma endregion
#pragma region Operators

ResGroup& ResGroup::operator=(ResGroup const&)
{
	return (*this);
}

Resource const* ResGroup::operator[](std::string const& _resName) const
{
	std::vector<Resource>::const_iterator it = m_resources.begin();
	for (int i = 0; it != m_resources.end(); ++it, i++)
		if (it->Name.compare(_resName) == 0)
			return (&m_resources.at(i));
	return (NULL);
}

#pragma endregion

bool ResGroup::AddResource(std::string const& _name, std::string const& _path)
{
	Resource r(_name, _path);
	if (std::find(m_resources.begin(), m_resources.end(), r) != m_resources.end())
		return (false);
	m_resources.push_back(r);
	return (true);
}

bool ResGroup::RemoveResource(std::string const& _name)
{
	Resource const* r = this->operator[](_name);
	std::vector<Resource>::iterator it =
		std::find(m_resources.begin(), m_resources.end(), *r);
	if (it == m_resources.end())
		return (false);
	m_resources.erase(it);
	return (true);
}

void ResGroup::Clear()
{
	m_resources.clear();
}

#pragma region Getters & Setters

std::string const& ResGroup::GetName() const
{
	return (m_name);
}

std::vector<Resource> const& ResGroup::GetResources() const
{
	return (m_resources);
}

#pragma endregion
#pragma endregion
#pragma region classe ResGroupManager

std::string const ResGroupManager::DEFAULT_GROUP = "default";
ResGroupManager& ResGroupManager::m_instance = ResGroupManager();
ResGroup ResGroupManager::m_defaultGroup(ResGroupManager::DEFAULT_GROUP);

#pragma region CTOR & DTOR

ResGroupManager::ResGroupManager()
{
	CTOR("ResGroupManager", this);
	this->Add(ResGroupManager::DEFAULT_GROUP, &ResGroupManager::m_defaultGroup,
			  NULL, NULL, false);
}

ResGroupManager::ResGroupManager(ResGroupManager const&)
{
}

ResGroupManager::~ResGroupManager()
{
	DTOR("ResGroupManager", this);
}

#pragma endregion
#pragma region Operators

ResGroupManager& ResGroupManager::operator=(ResGroupManager const&)
{
	return (*this);
}

#pragma endregion

ResGroupManager& ResGroupManager::Instance()
{
	return (ResGroupManager::m_instance);
}

void ResGroupManager::Manage(IArgs* _args)
{
}

bool ResGroupManager::AddResource(std::string const& _resName, std::string const& _path,
								  std::string const& _groupName)
{
	Elements::iterator it = m_elements.begin();
	for (; it != m_elements.end(); ++it)
		if (it->second.first->Object->GetName().compare(_groupName) == 0)
			return (it->second.first->Object->AddResource(_resName, _path));
	return (false);
}

Resource const& ResGroupManager::GetResource(std::string const& _groupName,
											 std::string const& _resName)
{
	Resource *r = new Resource("", "");
	return (*r);
}

#pragma endregion

NS_END(UTILS)
NS_END(SDL)
